// user/auctionAdd/auctionAdd.js
import http from '../../http'
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		fileList: [],
		fileList1: [],
		userInfo: {},
		info: {
			name: '',
			rank: '',
			identity_just: '',
			identity_against: ''
		},
		isLegal: false, //验证
		tips: '' //验证信息
	},
	onAgreementTap(e) {
		const type = e.currentTarget.dataset.type;

		// 确保URL格式正确，包含type参数
		if (type === 'service') {
			wx.navigateTo({
				url: '/user/service/service?type=1' // 明确传递type=1
			});
		} else if (type === 'privacy') {
			wx.navigateTo({
				url: '/user/privacy/privacy?type=2' // 明确传递type=2
			});
		}
	},
	submit() {
		if (this.data.isLegal) {
			http.post({
				url: '/User/addRealname',
				data: {
					user_id: wx.getStorageSync('userId'),
					identity_just: this.data.info.identity_just,
					identity_against: this.data.info.identity_against,
					type: '身份证',
					name: this.data.info.name,
					identity_number: this.data.info.rank
				},
				then: (res) => {
					console.log('实名认证', res);
					if (res.data.code == 1) {
						wx.showToast({
							title: '认证成功',
							icon: 'success',
							duration: 2000
						})
					} else {
						wx.showToast({
							title: res.data.msg,
							icon: 'error',
							duration: 2000
						})
					}
				}
			})
		} else {
			wx.showToast({
				title: this.data.tips,
				icon: 'error',
				duration: 2000
			})
		}
	},
	onInputValue(e) {
		// console.log(e.detail);
		let data = e.currentTarget.dataset.type
		this.setData({
			[`info.${data}`]: e.detail.value
		}, () => {
			const {
				isLegal,
				tips
			} = this.onVerifyInputLegal();
			this.setData({
				isLegal: isLegal,
				tips: tips
			})
		})
	},
	onVerifyInputLegal() {
		const {
			name,
			rank,
			identity_just,
			identity_against
		} = this.data.info;
		if (!name || !name.trim()) {
			return {
				isLegal: false,
				tips: '请填写姓名',
			};
		}
		if (!identity_just || !identity_just.trim()) {
			return {
				isLegal: false,
				tips: '请上传身份证正面',
			};
		}
		if (!identity_against || !identity_against.trim()) {
			return {
				isLegal: false,
				tips: '请上传身份证反面',
			};
		}
		if (!rank || !rank.trim()) {
			return {
				isLegal: false,
				tips: '请填写身份证',
			};
		}
		return {
			isLegal: true,
			tips: '添加成功',
		};
	},
	handleAdd(e) {
		const files = e.detail.files;
		//每次选择图片都上传，展示每次上传图片的进度
		files.forEach(file => this.uploadFile(file))
	},
	handleAdd1(e) {
		const files = e.detail.files;
		//每次选择图片都上传，展示每次上传图片的进度
		files.forEach(file => this.uploadFile1(file))
	},
	uploadFile(file) {
		http.file({
			url: '/Update/uploads',
			data: {},
			img: file.url,
			then: (res) => {
				console.log('上传文件', res);
				let data = JSON.parse(res.data)
				console.log(data);
				if (data.code == 1) {
					let fileList = this.data.fileList
					this.setData({
						fileList: [...fileList, file], // 此时设置了
						'info.identity_just': data.data,
					});
				} else {
					wx.showToast({
						title: data.msg,
						icon: 'error',
					})
				}
			},
			catch: (err) => {}
		})
	},
	uploadFile1(file) {
		http.file({
			url: '/Update/uploads',
			data: {},
			img: file.url,
			then: (res) => {
				console.log('上传文件', res);
				let data = JSON.parse(res.data)
				console.log(data);
				if (data.code == 1) {
					let fileList1 = this.data.fileList1
					this.setData({
						fileList1: [...fileList1, file], // 此时设置了
						'info.identity_against': data.data,
					});
				} else {
					wx.showToast({
						title: data.msg,
						icon: 'error',
					})
				}
			},
			catch: (err) => {}
		})
	},
	handleRemove(e) {
		const {
			index
		} = e.detail;
		const {
			fileList
		} = this.data;

		fileList.splice(index, 1);
		this.setData({
			fileList,
		});
	},
	handleRemove(e) {
		const {
			index
		} = e.detail;
		const {
			fileList1
		} = this.data;

		fileList1.splice(index, 1);
		this.setData({
			fileList1,
		});
	},
	gain() {

	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		this.gain()
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
		// http.post({
		//   url: '/User/info',
		//   data: {
		//     id: wx.getStorageSync('userId')
		//   },
		//   then: (res) => {
		//     console.log('用户信息', res);
		//     let data = res.data.data
		//     this.setData({
		//       userInfo: data,
		//     }, () => {
		//       if (data.authentication) {
		//         http.post({
		//           url: '/User/realnameDetail',
		//           data: {
		//             id: wx.getStorageSync('userId')
		//           },
		//           then: (res) => {
		//             console.log('实名信息', res);
		//           }
		//         })
		//       }
		//     })
		//   }
		// })
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})